varying vec3 normale;
varying vec4 positione;
uniform int numLights;

void main()
{
	vec3 norm = normalize(normale);
	vec3 viewv = -normalize(positione.xyz);
	
	vec3 color = vec3(0.0, 0.0, 0.0);
	if(numLights > 0) {
		// Light vector
		vec3 lightv = normalize( gl_LightSource[0].position.xyz - positione.xyz);
		vec3 halfv = normalize(lightv + viewv);
		// diffuse reflection 
		vec4 diffuse = max(0.0, dot(lightv, norm))*gl_FrontMaterial.diffuse*gl_LightSource[0].diffuse;
		// ambient reflection 
		vec4 ambient = gl_FrontMaterial.ambient*gl_LightSource[0].ambient;
	
	
		// specular reflection 
		vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
		if( dot(lightv, viewv) > 0.0) {
	  		specular = pow(max(0.0, dot(norm, halfv)), gl_FrontMaterial.shininess) * 
	  		           gl_FrontMaterial.specular*gl_LightSource[0].specular;
	    }
		color += clamp( vec3(ambient + diffuse + specular), 0.0, 1.0);
	}
	// second light
	if(numLights > 1) {
		// Light vector
		vec3 lightv = normalize( gl_LightSource[1].position.xyz - positione.xyz);
		vec3 halfv = normalize(lightv + viewv);
		// diffuse reflection 
		vec4 diffuse = max(0.0, dot(lightv, norm))*gl_FrontMaterial.diffuse*gl_LightSource[1].diffuse;
		// ambient reflection 
		vec4 ambient = gl_FrontMaterial.ambient*gl_LightSource[1].ambient;
	
	
		// specular reflection 
		vec4 specular = vec4(0.0, 0.0, 0.0, 1.0);
		if( dot(lightv, viewv) > 0.0) {
	  		specular = pow(max(0.0, dot(norm, halfv)), gl_FrontMaterial.shininess) * 
	  		           gl_FrontMaterial.specular*gl_LightSource[1].specular;
	    }
		color += clamp( vec3(ambient + diffuse + specular), 0.0, 1.0);
	}
	gl_FragColor = vec4(color, 1.0);
}